#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <strings.h>

/*
通过getaddrinfo函数获取IP地址和获取服务名获得端口号。
*/
int main(int argc,char * argv[])
{
    assert(argc == 2);
    char *host = argv[1];

    struct addrinfo hints;
    struct addrinfo* res;
    bzero(&hints,sizeof(hints));
    hints.ai_socktype = SOCK_STREAM;
    int status = getaddrinfo("host1","daytime",&hints,&res);
    if(status = 0){
        fprintf(stderr,"getaddrinfo error:%s\n",gai_strerror(status));
        return 1;
    }

    int sockfd = socket(res->ai_family,res->ai_socktype,res->ai_protocol);
    int result = connect(sockfd,res->ai_addr,res->ai_addrlen);
    assert(result!=-1);

    char buffer[128];
    result = read(sockfd,buffer,sizeof(buffer));
    assert(result >0);
    buffer[result] = '\0';
    printf("the day time is:%s",buffer);
    close(sockfd);
    freeaddrinfo(res);
    return 0;
}